package com.yc.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.bean.Addr;
import com.yc.biz.AddrBiz;
import com.yc.dao.DBHelper;

public class AddrBizImpl implements AddrBiz {

	DBHelper db=new DBHelper();
	
	@Override//查询所有的地址
	public List<Map<String, String>> findAllAddr(int uid) {
		String sql="select * from addr where uid="+uid;
		return db.findAll(sql, null);
	}

	@Override//设置默认地址
	public int setDefault(int uid, int aid) {
		//先将所有地址设为0 
		String sql="update addr set status=0 where uid="+uid;
		db.doUpdate(sql, null);
		
		String sql1="update addr set status=1 where uid="+uid+" and aid="+aid;
		return db.doUpdate(sql1, null);
	}

	@Override//添加地址
	public int addAddr(int uid, String uname, String tel, String prov, String city, String area, String addr) {
		String sql="insert into addr values(null,?,?,?,?,?,?,?,0)";
		List<Object> params=new ArrayList<Object>();
		params.add(uid);
		params.add(uname);
		params.add(tel);
		params.add(prov);
		params.add(city);
		params.add(area);
		params.add(addr);
		
		return db.doUpdate(sql, params);
	}

	@Override//查询默认地址
	public List<Map<String, String>> getDafalutAddr(int uid) {
		String sql="select * from addr where uid="+uid+" and status=1";
		List<Addr> addr=db.find(sql, null, Addr.class);
		return db.findAll(sql, null);
	}

	@Override
	public List<Map<String, String>> getProv() {
		String sql = "SELECT * FROM t_address_province";
		List<Map<String, String>> list = db.findAll(sql, null);
		return list;
	}

	@Override
	public List<Map<String, String>> getCity(int pcode) {
		String sql = "SELECT * FROM t_address_city WHERE provinceCode="+pcode;
		List<Map<String, String>> list = db.findAll(sql, null);
		return list;
	}

	@Override
	public List<Map<String, String>> getArea(int ccode) {
		String sql = "SELECT * FROM t_address_town WHERE cityCode="+ccode;
		List<Map<String, String>> list = db.findAll(sql, null);
		return list;
	}

}
